<?php 
namespace app\admin\controller;
use think\Controller;

class Type extends Controller{

	public function index(){
		$res = db('type')->paginate(15);
		$this->assign([
			'res'	=>$res,
		]);
		return view();
	}

	public function add(){
		if(request()->isPost()){
			$data = input('post.');
			$res = db('type')->insert($data);
			if($res){
				$this->success('商品类型添加成功！',url('index'));
			}else{
				$this->error('商品类型添加失败！');
			}
		}
		return view();
	}

	public function edit($id){
		if(request()->isPost()){
			$data = input('post.');
			$res = db('type')->update($data);
			if($res !== false){
				$this->success('商品类型修改成功！',url('index'));
			}else{
				$this->error('商品类型修改失败!');
			}
		}
		$res = db('type')->find($id);
		$this->assign([
			'res'	=>$res,
		]);
		return view();
	}

	public function del($id){
		$attr_res = db('attr')->where(["type_id"=>$id])->select();
		$total_res=array();
		foreach($attr_res as $k=>$v){
			$total[] = $v['id'];
		}
		if($total){
			db('attr')->delete($total);
		}
		$res = db('type')->delete($id);
		if($res){
			$this->success('商品类型删除成功！',url('index'));
		}else{
			$this->error('商品类型删除失败！');
		}
	}
}